<template>
  <div>
    <el-button type="success" @click="addBtnTop">新增</el-button>
    <el-button type="danger" @click="updAll">批量删除</el-button>
    <!--  表格 -->
    <el-table
      :data="tlist"
      @selection-change="handleSelectionChange"
      border
      style="width: 100%;margin-top:10px"
      max-height="500px"
    >
      <el-table-column fixed type="selection" width="55"></el-table-column>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="title" label="标题" width="600"></el-table-column>
      <el-table-column prop="imgurl" label="图片路径" width="600"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleClicks(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="delAll(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 抽屉 -->
    <el-drawer
      size="30%"
      style="overflow-y:auto;max-height:100%;"
      :visible.sync="drawer"
      :direction="direction"
      padding-right="10px"
    >
      <el-form ref="form" :model="form" label-width="80px" style="padding:10px">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="图片路径">
          <!-- <el-input v-model="form.imgurl"></el-input> -->
          <el-upload
            class="avatar-uploader"
            action="http://localhost:2000/api/upload/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            name="file"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确定</el-button>
          <el-button @click="del">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { mapActions, mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      drawer: false,
      direction: "rtl",
      form: {
        imgurl: "",
      },
      imageUrl: "",
      checkAll: [],
    };
  },
  mounted() {
    this.tgetList();
    this.tgetListfirst();
  },
  methods: {
    ...mapActions("three", [
      "tgetList",
      "addListFun",
      "delLists",
      "tgetListfirst",
    ]),
    // 图片上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.imgurl = res.data;
      this.$set(this.form, "imgurl", res.data);
    },
    beforeAvatarUpload(file) {},
    //确定
    onSubmit() {
      if (this.form.id) {
        this.$message({
          message: "恭喜你，修改成功",
          type: "success",
        });
      } else {
        this.$message({
          message: "恭喜你，新增成功",
          type: "success",
        });
      }
      this.addListFun(this.form);
      this.form = {};
      this.drawer = false;
    },
    //编辑
    handleClicks(val) {
      this.drawer = true;
      this.form = JSON.parse(JSON.stringify(val));
      this.imageUrl = this.form.imgurl;
    },
    //删除
    delAll(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.delLists(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //批量删除
    updAll() {
      let ids = this.checkAll;
      console.log(ids);
      let arr = [];
      if (ids.length) {
        ids.forEach((v) => {
          arr.push(v.id);
        });
        this.delAll(arr.join(","));
      } else {
        this.$message({
          message: "请选择要删除的项",
        });
      }
    },
    //新增
    addBtnTop() {
      this.drawer = true;
      this.form = {};
    },
    //复选框
    handleSelectionChange(val) {
      this.checkAll = val;
    },
    //取消
    del() {
      this.drawer = false;
      this.form = {};
    },
  },
  computed: {
    ...mapState("three", ["tlist", "cntitle"]),
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
